package com.ha8.netease.views
{
	import com.ha8.netease.PublicNotify;
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	import com.rogo.utils.DesktopUtil;
	import com.rogo.utils.DockUtil;
	
	import flash.desktop.NativeApplication;
	import flash.display.NativeMenu;
	import flash.display.NativeMenuItem;
	import flash.display.NativeWindowDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NativeWindowDisplayStateEvent;
	import flash.media.Video;
	import flash.system.Capabilities;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	import mx.core.FlexGlobals;
	
	public class Ha8NeteaseMediator extends Mediator
	{
		public function Ha8NeteaseMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'ha8NeteaseMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		private var appWidth:Number;//分辨率:宽
		private var appHeight:Number;//分辨率:高
		private var adsorption:String = '';//吸附的轴
		private var isAdsor:Boolean = true;//是否缩到屏幕里面
		private var interval:uint; 
		
		private var dockUtil:DockUtil;
		
		[Embed(source="assets/icon/16.png")]
		private var icon16:Class;
		
		[Embed(source="assets/icon/32.png")]
		private var icon32:Class;
		
		[Embed(source="assets/icon/48.png")]
		private var icon48:Class;
		
		[Embed(source="assets/icon/128.png")]
		private var icon128:Class;
		
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function onRegister():void
		{
			// 窗口移到桌面中间
			DesktopUtil.moveToCenter(view.nativeWindow);
			
			// 托盘停靠支持
			dockUtil = new DockUtil(view.nativeWindow);
			dockUtil.icon16 = icon16;  
			dockUtil.icon32 = icon32;
			dockUtil.icon48 = icon48;
			dockUtil.icon128 = icon128;
			dockUtil.menu = createDockMenu();
			dockUtil.tooltip = 'Ha8微博';
			
			appWidth = flash.system.Capabilities.screenResolutionX;
			appHeight = flash.system.Capabilities.screenResolutionY;
			
			view.addEventListener(MouseEvent.ROLL_OVER, view_OverHandler);
			view.addEventListener(MouseEvent.ROLL_OUT, view_OutHandler);
			view.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING,changHandler);
			view.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGE,changHandler);
			view.addEventListener(Event.CLOSING, closingHander);
			view.addEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
		}

		override public function listNotificationInterests():Array
		{
			return [PublicNotify.LOGIN_RESULT, PublicNotify.LOGIN_ERROR, PublicNotify.QUEIT_NOTIFY];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			switch (notification.name)
			{
				case PublicNotify.LOGIN_RESULT:
					view.currentState = 'login';
					break;
				case PublicNotify.LOGIN_ERROR:
					view.currentState = 'logout';
					break;
				case PublicNotify.QUEIT_NOTIFY:
					exit_selectHandler(null);
					break;
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function get view():Ha8Netease
		{
			return viewComponent as Ha8Netease;
		}
		
		/**
		 * 创建停靠菜单（右键停靠图标时显示的菜单）
		 */		
		private function createDockMenu():NativeMenu
		{
			var menu:NativeMenu = new NativeMenu();
			var exit:NativeMenuItem = new NativeMenuItem('退出');
			exit.addEventListener(Event.SELECT, exit_selectHandler);
			menu.addItem(exit);
			
			return menu;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function closingHander(event:Event):void
		{
			event.preventDefault();
			dockUtil.dock();
		}
		
		private function exit_selectHandler(e:Event):void
		{
			// bug here
			view.nativeWindow.close();
			
			//close Windows
			for (var i:int = 0; i < NativeApplication.nativeApplication.openedWindows.length; i++)
			{
				NativeApplication.nativeApplication.openedWindows[i].close();
			}
		}
		
		private function addToStageHandler(e:Event):void
		{
			view.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		
		private function mouseUpHandler(e:MouseEvent):void
		{
			view.autoLayout = true;
		}
		
		private function view_OverHandler(e:MouseEvent):void//是否吸附进去<吸附进:出来>
		{
			if (isAdsor == false)
			{
				return;
			}
			
			var x1:Number = FlexGlobals.topLevelApplication.stage.nativeWindow.x;
			var y1:Number = FlexGlobals.topLevelApplication.stage.nativeWindow.y;
			
			if (adsorption == 'x')
			{
				if (x1 < 0)
				{
					TweenLite.to(FlexGlobals.topLevelApplication.stage.nativeWindow,0.4,{x:-1});
					view.alwaysInFront = false;
				}
				else if (x1+ view.width> appWidth)
				{
					TweenLite.to(FlexGlobals.topLevelApplication.stage.nativeWindow,0.4,{x:appWidth-view.width+1});
					view.alwaysInFront = false;
				}
			}
			
			if (adsorption == 'y')
			{
				if (y1 < 0)
				{
					TweenLite.to(FlexGlobals.topLevelApplication.stage.nativeWindow,0.4,{y:-1});
					view.alwaysInFront = false;
				}
			}
		}
		
		private function view_OutHandler(e:MouseEvent):void//是否在屏幕边缘<在:就吸附进去>
		{
			if (isAdsor == false)
			{
				return;
			}
			
			var x1:Number = FlexGlobals.topLevelApplication.stage.nativeWindow.x;
			var y1:Number = FlexGlobals.topLevelApplication.stage.nativeWindow.y;
			adsorption = 'null';
			
			if (x1 <= 0)
			{
				adsorption = 'x';
				TweenLite.to(FlexGlobals.topLevelApplication.stage.nativeWindow,0.4,{x:-view.width+2});
				view.alwaysInFront = true;
			}
			else if (x1+ view.width>= appWidth)
			{
				adsorption = 'x';
				TweenLite.to(FlexGlobals.topLevelApplication.stage.nativeWindow,0.4,{x:appWidth-2});
				view.alwaysInFront = true;
			}
			
			if (y1 <= 0 && adsorption == 'null')
			{
				adsorption = 'y';
				TweenLite.to(FlexGlobals.topLevelApplication.stage.nativeWindow,0.4,{y:-view.height+2});
				view.alwaysInFront = true;
			}
		}
		
		private function changHandler(event:NativeWindowDisplayStateEvent):void
		{
			if (event.type == NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING)//窗体正在变化
			{
				isAdsor = false;
			}
			else//窗体变化为
			{
				if (event.afterDisplayState == NativeWindowDisplayState.MAXIMIZED)
				{
					isAdsor = false;
				}
				else
				{
					isAdsor = false;
					interval = setInterval(getTiemHandler,1000);
				}
			}
		}
		
		private function getTiemHandler():void
		{
			clearInterval(interval);
			isAdsor = true;
		}
	}
}